// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.output.dom;

import static org.hypirinha.html.AttributeFactory.id;
import static org.hypirinha.html.ElementFactory.title;
import org.hypirinha.html.elements.Title;
import org.hypirinha.html.output.DomAdapter;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.w3c.dom.Document;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

public class EscapingTest {

    @Test
    public void shouldEscapeAttributesAndTextNodesSoThatDocumentStructureIsPreserved() throws Exception {
        Title content = title(id("\"quoted attribute\""));
        content.text("< less than & greater than >");

        Document document = new DomAdapter().asDocument(content);

        StringWriter stringWriter = new StringWriter();
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(stringWriter));
        assertTrue(stringWriter.toString().contains(
                "<title id=\"&quot;quoted attribute&quot;\">&lt; less than &amp; greater than &gt;</title>"));
    }
}